package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.IWechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信服务实现类
 *
 * @author itzq  // 替换为实际开发者的姓名或工号
 */

@Service
public class IWechatServiceImpl implements IWechatService {

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;

    private static final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";

    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    @Override
    public String getOpenId(String code) {
        Map<String, Object> params = new HashMap<>();
        params.put("appid", appid);
        params.put("secret", secret);
        params.put("js_code", code);
        params.put("grant_type", "authorization_code");

        HttpResponse response = HttpUtil.createRequest(Method.GET, OPENID_URL)
                .form(params)
                .execute();
        if (response.isOk()) {
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
//            if (jsonObject.getInt("errcode") == 0) { , 暂时先不判断了. 不知到为什么是null
            return jsonObject.getStr("openid");
//            }
        }
        return "";
    }

    @Override
    public String getPhone(String phoneCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("code", phoneCode);

        HttpResponse response = HttpUtil.createRequest(Method.POST, PHONE_URL + getToken())
                .body(JSONUtil.toJsonStr(params))  // 这里需要将参数转为JSON字符串
                .execute();
        if (response.isOk()) {
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
//            JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
//            String phoneNumber = phoneInfo.getStr("phoneNumber");  可以用getByPath一次获得
            String phoneNumber = jsonObject.getByPath("phone_info.phoneNumber", String.class);
            return phoneNumber;
        }
        return null;
    }

    private String getToken() { //  获取token, 补全Url
        Map<String, Object> params = new HashMap<>();
        params.put("appid", appid);
        params.put("secret", secret);
        params.put("grant_type", "client_credential");
        HttpResponse response1 = HttpUtil.createRequest(Method.GET, TOKEN_URL)
                .form(params)
                .execute();
        if ( response1.isOk()){
            String body = response1.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            return jsonObject.getStr("access_token");
        }
        return null;
    }
}
